//
//  XWUserAccountViewModels.swift
//  新浪 weibo
//
//  Created by xuwei on 16/1/24.
//  Copyright © 2016年 xuwei. All rights reserved.
//

import UIKit
import SVProgressHUD
class XWUserAccountViewModel: NSObject {

    
    static let shareViewModel:XWUserAccountViewModel = {
       let viewModel = XWUserAccountViewModel()
        return viewModel
    }()
    
    //归档 解档的路径
    private var archivePath:String{
        return (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.archive")
    }
    
    
    
    
    func loadAccessToken(code:String,finished:(isSuccess:Bool)->()){
            let urlString = "https://api.weibo.com/oauth2/access_token"
            
            let params = [
                
                "client_id" : WB_CLIENT_ID,
                "client_secret" : WB_CLIENT_SECRET,
                "grant_type": "authorization_code",
                "code": code,
                "redirect_uri" : WB_REDIRECT_URL
            ]
            XWNetworkTools.sharedTools.request(.POST, urlString: urlString, parameters: params) { (response, error) -> () in
                if error != nil {
                    print(error)
                    return
                }
                
                guard let dict = response as? [String:AnyObject]
                    else{
                        print("返回的数据不是字典")
                        return
                }
                let  account = XWUserAccount(dict: dict)
                
                self.loadUserInfo(account, finished: finished)
            }
        }
        
    private func loadUserInfo(account:XWUserAccount,finished:(isSuccess :Bool)->()){
        
            let urlString = "https://api.weibo.com/2/users/show.json"
            
            let params = [
                "access_token":account.access_token!,
                "uid":account.uid!
            ]
            XWNetworkTools.sharedTools.request(urlString: urlString, parameters: params) { (response, error) -> () in
                if error != nil{
                    print(error)
                    SVProgressHUD.showErrorWithStatus("网络异常")
                    return
                }
              
                
                guard let dict = response as? [String:AnyObject]
                    else{
                        finished(isSuccess: false)
                        return
                }
                account.avatar_large = dict["avatar_large"] as? String
                account.screen_name = dict["screen_name"] as? String
                
                print(account)
           
                finished(isSuccess: true)
                //归档路径
                let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.arhive")
                
                NSKeyedArchiver.archiveRootObject(account, toFile: path)
                
            }
        }
}

extension XWUserAccountViewModel {
    private func saveUserAccount(account:XWUserAccount){
        print(archivePath)
        
        NSKeyedArchiver.archiveRootObject(account, toFile: archivePath)
    }
    
    //解档数据
    func userAccount() -> XWUserAccount? {
        return NSKeyedUnarchiver.unarchiveObjectWithFile(archivePath) as? XWUserAccount
    }
    
}
